//
//  productSaveView.m
//  XYMaterial
//
//  Created by 肖扬 on 15/12/30.
//  Copyright © 2015年 肖扬. All rights reserved.
//

#import "productSaveView.h"

@implementation productSaveView
@synthesize saveButton,telButton,supply_id,material_id,is_favor,delegate;
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.saveButton=[TitleImageButton createBtnFrame:CGRectMake(10, (43 - 26)/2, 19, 17) title:@"" imageName:@"商品详情收藏" selecetdImage:@"商品详情收藏后" titleColor:UIColorFromRGB(0xadadad) target:self action:@selector(clickThisBtn:) style:TitleImageButtonDistributeStyleImageTopAndBottom tag:0];
        self.saveButton.textAlignment=NSTextAlignmentCenter;
        self.saveButton.imageMode=UIViewContentModeTop;
        self.saveButton.imageRatio=0.6;
        [self.saveButton setTitleColor:UIColorFromRGB(0xadadad) forState:0];
        [self.saveButton setTitleColor:UIColorFromRGB(0xadadad) forState:UIControlStateSelected];
        [self addSubview:self.saveButton];
        
        self.telButton = [self createButtonFrame:CGRectMake(45, (43 - 31)/2, SWIDTH - 55, 31) title:@"010-53519988" viewController:self selector:@selector(telButtonClick:) tag:0];
        Radio(self.telButton, clearColor);
        [self.telButton setBackgroundColor:UIColorFromRGB(0x247ac4)];
        [self addSubview:self.telButton];
        if ([self.is_favor integerValue] == 0) {
            self.saveButton.selected = NO;
        }else{
            self.saveButton.selected = YES;
        }
    }
    return self;
}
-(void)telButtonClick:(UIButton *)sender{
     NSString *num = [[NSString alloc] initWithFormat:@"tel://%@",sender.titleLabel.text];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]];
}
-(void)changeSaveState{
    if ([self.is_favor integerValue] == 0) {
        self.saveButton.selected = NO;
    }else{
        self.saveButton.selected = YES;
    }
}
-(void)clickThisBtn:(UIButton *)sender{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] isEqualToString:@"0"] || [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] == nil) {
        if (self.delegate!=nil && [self.delegate respondsToSelector:@selector(isLogin)]) {
            [self.delegate isLogin];
            return;
        }
    }
    if (sender.selected == NO) {
        [self addFavor];
    }else{
        [self delFavor];
    }
    NSLog(@"收藏");
}
/**
 *  添加收藏
 *
 *  @param 货源id，品种id
 */
-(void)addFavor{
    TIME
    HEADDIC(nowTime, ADDFAVOR);
    NSMutableDictionary *body = [NSMutableDictionary dictionary];
    [body setObject:self.supply_id forKey:@"supply_id"];
    [body setObject:self.material_id forKey:@"material_id"];
    NSString *result = [Unity requestSomeApiData:HEAD withBody:body];
    NSLog(@"result__%@",result);
    NSMutableDictionary　*json = [result objectFromJSONString];
    NSMutableDictionary *data = [json objectForKey:@"data"];
    NSInteger errCode = [[json objectForKey:@"err_code"] integerValue];
    if (errCode ==0) {
        saveButton.selected = YES;
        SHOW_TITLEALERT(@"提示", @"收藏成功");
    }else{
        saveButton.selected = NO;
        if (errCode == 101) {
            SHOW_ALERT([json objectForKey:@"err_message"]);
            return;
        }
    }
}
/**
 *  删除收藏
 *
 *  @param 货源id，品种id
 */
-(void)delFavor{
    TIME
    HEADDIC(nowTime, DELFAVOR);
    NSMutableDictionary *body = [NSMutableDictionary dictionary];
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
    NSMutableArray *ids = [NSMutableArray array];
    [tempDic setObject:self.supply_id forKey:@"supply_id"];
    [tempDic setObject:self.material_id forKey:@"material_id"];
    [ids addObject:tempDic];
    [body setObject:ids forKey:@"ids"];
    NSString *result = [Unity requestSomeApiData:HEAD withBody:body];
    NSLog(@"result__%@",result);
    NSMutableDictionary　*json = [result objectFromJSONString];
    NSMutableDictionary *data = [json objectForKey:@"data"];
    NSInteger errCode = [[json objectForKey:@"err_code"] integerValue];
    if (errCode ==0) {
        saveButton.selected = NO;
        SHOW_TITLEALERT(@"提示", @"取消收藏");
    }else{
        saveButton.selected = YES;
        if (errCode == 101) {
            SHOW_ALERT([json objectForKey:@"err_message"]);
            return;
        }
    }
}
//创建UILabel  tag值为三位数
- (UILabel *)createLabelFrame:(CGRect)frame
                        title:(NSString *)title
                   titleColor:(UIColor *)color
                          tag:(NSInteger)tag{
    UILabel *tempLabel = [[UILabel alloc]initWithFrame:frame];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setText:title];
    [tempLabel setTag:tag];
    [tempLabel setTextColor:color];
    return tempLabel;
}
//创建UIButton tag值为四位数
- (UIButton *)createButtonFrame:(CGRect)frame
                          title:(NSString *)title
                 viewController:(id)viewController
                       selector:(SEL)selector
                            tag:(NSInteger)tag{
    UIButton *tempButton = [UIButton buttonWithType:0];
    [tempButton setFrame:frame];
    [tempButton setTag:tag];
    [tempButton setBackgroundColor:[UIColor clearColor]];
    [tempButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tempButton setTitle:title forState:UIControlStateNormal];
    [tempButton addTarget:viewController action:selector forControlEvents:UIControlEventTouchUpInside];
    return tempButton;
}
-(UIButton *)createClickButton:(CGRect)frame
                           tag:(NSInteger)tag
                      selector:(SEL)selector
                viewController:(id)viewController
                         title:(NSString *)title{
    UIButton *tempButton = [self createButtonFrame:frame title:@"" viewController:self selector:selector tag:tag];
    UILabel *titleLab = [self createLabelFrame:frame title:title titleColor:UIColorFromRGB(0xadadad) tag:0];
    [tempButton addSubview:titleLab];
    
    return tempButton;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
